package com.zx.tradeclient.ctrl.index3;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.base.util.URLHelper;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.index5.LoginActivity;
import com.zx.tradeclient.entity.CompanyInfo;

public class CompanyInfoActivity extends BaseActivity implements OnClickListener {
	/** Called when the activity is first created. */

	private String mCompanyID = "";

	private ProgressDialog progressDialog;

	private ImageView company_icon;
	private TextView company_name;
	private TextView company_category;
	private TextView company_phone;
	private TextView company_address;

	private RelativeLayout company_about;
	private RelativeLayout company_product;
	private RelativeLayout company_share;
	private RelativeLayout company_message;

	private TradeApp app;

	private CompanyInfo info = new CompanyInfo();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.company_info);

		if (!(ActivityTaskManager.getInstance().containsName("CompanyInfoActivity"))) {
			ActivityTaskManager.getInstance().putActivity("CompanyInfoActivity", this);
		}

		init();

	}

	private void init() {

		app = (TradeApp) getMyApplication();

		mCompanyID = getIntent().getStringExtra("companyID");

		setHeader(0);
		header_right.setText("收藏");
		header_right.setVisibility(View.VISIBLE);
		header_right.setOnClickListener(this);

		company_icon = (ImageView) findViewById(R.id.company_info_icon);
		company_name = (TextView) findViewById(R.id.company_info_name);
		company_category = (TextView) findViewById(R.id.company_info_category);
		company_phone = (TextView) findViewById(R.id.company_info_phone);
		company_address = (TextView) findViewById(R.id.company_info_address);

		company_about = (RelativeLayout) findViewById(R.id.company_info_about);
		company_about.setOnClickListener(this);
		company_product = (RelativeLayout) findViewById(R.id.company_info_product);
		company_product.setOnClickListener(this);
		company_share = (RelativeLayout) findViewById(R.id.company_info_share);
		company_share.setOnClickListener(this);
		company_message = (RelativeLayout) findViewById(R.id.company_info_message);
		company_message.setOnClickListener(this);

		progressDialog = ProgressDialog.show(CompanyInfoActivity.this, null, "正在获取数据，请稍后.....", true, true);

		getCompanyInfo();
	}

	private void getCompanyInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "companyDetail");
		map.put("id", mCompanyID);

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(CompanyInfoActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

	private void parseJson(JSONObject response) {
		try {
			JSONObject object = response;
			String succeed = object.getString("succeed");

			if (succeed.equals("000")) {

				info.setCom_id(object.getString("id"));

				app.imageLoader.DisplayImage(URLHelper.mPrefixImg + object.getString("photo"), company_icon, R.drawable.goods_icon_default);
				info.setCom_pic(object.getString("photo"));

				company_name.setText(object.getString("name"));
				info.setCom_name(object.getString("name"));

				company_phone.setText(object.getString("telphone"));
				info.setCom_phone(object.getString("telphone"));

				company_category.setText(object.getString("bySector"));
				info.setCom_industry(object.getString("bySector"));

				info.setCom_intro(object.getString("companyIntro"));

				company_address.setText(object.getString("address"));
				info.setCom_address(object.getString("address"));

			} else {
				Toast toast2 = Toast.makeText(CompanyInfoActivity.this, "无数据", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
			}

			progressDialog.dismiss();

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {
			goback();
		} else if (v == header_right) {

			if (app.user.getId().equals("")) {
				Intent intent = new Intent(CompanyInfoActivity.this, LoginActivity.class);
				startActivity(intent);
			} else {
				progressDialog = ProgressDialog.show(CompanyInfoActivity.this, null, "正在提交数据，请稍后.....", true, true);

				companyFavorite();
			}

		} else if (v == company_about) {

			Intent intent3 = new Intent(CompanyInfoActivity.this, SummaryActivity.class);

			intent3.putExtra("companyID", mCompanyID);
			startActivity(intent3);

		} else if (v == company_product) {
			Intent intent3 = new Intent(CompanyInfoActivity.this, ProductListActivity.class);
			intent3.putExtra("companyID", mCompanyID);

			startActivity(intent3);
		} else if (v == company_share) {

			Intent intent = new Intent(Intent.ACTION_SEND);

			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_TEXT, "");

			String fx = " " + " \n"+company_name.getText()+ " \n"+company_phone.getText();
			intent.putExtra(Intent.EXTRA_TEXT, fx);
			startActivity(Intent.createChooser(intent, getTitle()));

		} else if (v == company_message) {

			if(app.user.getId().equals("")){
				Intent intent = new Intent(CompanyInfoActivity.this, LoginActivity.class);
				startActivity(intent);
			}else{
				Intent intent = new Intent(CompanyInfoActivity.this, LeaveMessageActivity.class);
				intent.putExtra("mycompanyID", app.user.getId());
				intent.putExtra("sendcompanyID", mCompanyID);
				intent.putExtra("sendcompanyName", info.getCom_name());
				intent.putExtra("messageID", "");
				startActivity(intent);
			}
			
		}

	}

	private void companyFavorite() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "favourites");
		map.put("userId", app.user.getId());
		map.put("type", "5");
		map.put("infoId", mCompanyID);
		map.put("infoName", info.getCom_name());

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				// 解析数据
				try {
					String succeed = response.getString("succeed");

					if (succeed.equals("000")) {
						Toast toast2 = Toast.makeText(CompanyInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();

					} else {
						Toast toast2 = Toast.makeText(CompanyInfoActivity.this, "无数据", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();
					}

					progressDialog.dismiss();

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(CompanyInfoActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

}